package com.controller;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.web.servlet.ModelAndView;

import com.controller.base.MultiMethodController;
import com.service.UniversalManager;

/**
 * 2010
 * **/

public class UniversalController extends MultiMethodController {
	private UniversalManager universalManager;

	java.io.Serializable obj;
	java.io.Serializable pk;

	public UniversalManager getUniversalManager() {
		return universalManager;
	}

	public void setUniversalManager(UniversalManager universalManager) {
		this.universalManager = universalManager;
	}

	public java.io.Serializable getObj() {
		return obj;
	}

	public void setObj(java.io.Serializable obj) {
		this.obj = obj;
	}

	public java.io.Serializable getPk() {
		return pk;
	}

	public void setPk(java.io.Serializable pk) {
		this.pk = pk;
	}

	/**
	 * @param json
	 *            object
	 * 
	 * **/
	public ModelAndView getInsert(HttpServletRequest request,
			HttpServletResponse response) {
		Map map = new HashMap();
		try {
			Object obj = this.getObjectFromJson(request, response, this.obj);
			universalManager.insert(obj);
			map.put("success", true);
			map.put("o", obj);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
		}
		JSONObject jsonObject = JSONObject.fromObject(map);
		getMessage(response, jsonObject.toString());
		return null;
	}

	/**
	 * @param JsonObject
	 * 
	 * **/
	public ModelAndView getUpdate(HttpServletRequest request,
			HttpServletResponse response) {
		Map map = new HashMap();
		try {
			Object obj = this.getObjectFromJson(request, response, this.obj);
			universalManager.update(obj);
			map.put("success", true);
			map.put("o", obj);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
		}
		getMessage(response, getJsonFromJSONObject(map));
		return null;
	}

	/**
	 * @param id
	 * 
	 * **/
	public ModelAndView getDelete(HttpServletRequest request,
			HttpServletResponse response) {
		Map map = new HashMap();
		Serializable id = null;
		String msg="";
		try {
			if (this.pk == null)
				id = request.getParameter("id");//;id=12
			else
				id = (Serializable) this.getObjectFromJson(request, response,
						this.pk);//;key1=111&key2=222
			System.out.println(Class.forName(this.obj
					.toString()));
			Object obj = universalManager.findByID(Class.forName(this.obj
					.toString()), id);
			universalManager.delete(obj);
//			map.put("success", true);
			msg="删除成功！";
		} catch (Exception e) {
			e.printStackTrace();
//			map.put("success", false);
			msg="删除失败！";
		}
//		getMessage(response, getJsonFromJSONObject(map));
		getMessage(response, msg);
		return null;
	}

	/**
	 * @param id
	 * **/
	public ModelAndView getObject(HttpServletRequest request,
			HttpServletResponse response) {
		Map map = new HashMap();
		Serializable id = null;
		Object obj=null;
		try {
			if (this.pk == null)
				id = request.getParameter("id");//;id=12
			else
				id = (Serializable) this.getObjectFromJson(request, response,
						this.pk);//key1=111&key2=222
				obj = universalManager.findByID(Class.forName(this.obj
					.toString()), id);
			logger.info(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		getMessage(response, getJsonFromJSONArray(obj));
		return null;
	}

	/**
	 * 
	 * **/
	public ModelAndView getListAll(HttpServletRequest request,
			HttpServletResponse response) {
		Map map = new HashMap();
		List list = null;
		try {
			list = universalManager.findAll(Class.forName(this.obj.toString()));
			logger.info(list);
			map.put("success", true);
			map.put("result", list);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
		}
		getMessage(response, getJsonFromJSONArray(map));
		return null;
	}
	//返回当前页码
	public int getPageNo(HttpServletRequest request){
		try{
			String pageNo=request.getParameter("pageNo");
			if(pageNo==null||"".equals(pageNo))pageNo="1";
			return Integer.parseInt(pageNo);
		}catch(Exception e){
			e.printStackTrace();
			return 1;
		}
	}
}
